﻿using DTcms.Common;
using System;
using System.Data;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI
{
    public partial class BaseController : Controller
    {
        /// <summary>
        /// 查询数据 用于Tags
        /// </summary>
        public DataTable get_search_list(int site_id, int tags_id, int page, int pagesize, out int totalcount)
        {
            //创建一个DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns.Add("title", Type.GetType("System.String"));
            dt.Columns.Add("zhaiyao", Type.GetType("System.String"));
            dt.Columns.Add("channel_id", Type.GetType("System.String"));
            dt.Columns.Add("category_id", Type.GetType("System.String"));
            dt.Columns.Add("link_url", Type.GetType("System.String"));
            dt.Columns.Add("add_time", Type.GetType("System.String"));
            dt.Columns.Add("img_url", Type.GetType("System.String"));
            dt.Columns.Add("click", Type.GetType("System.Int32"));

            //创建一个DataSet,判断是使用Tags还是关健字查询
            DataSet ds = new BLL.article().ArticleSearch(site_id, tags_id, pagesize, page, "status=0", "add_time desc,id desc", out totalcount);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds.Tables[0].Rows[i];
                    string link_url = get_url_rewrite(Utils.StrToInt(dr1["channel_id"].ToString(), 0), dr1["call_index"].ToString(), Utils.StrToInt(dr1["id"].ToString(), 0));
                    if (!string.IsNullOrEmpty(link_url))
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = dr1["id"]; //自增ID
                        dr["title"] = dr1["title"]; //标题
                        dr["zhaiyao"] = dr1["zhaiyao"]; //摘要
                        dr["channel_id"] = dr1["channel_id"]; //频道ID
                        dr["category_id"] = dr1["category_id"]; //类别
                        dr["link_url"] = link_url; //链接地址
                        dr["img_url"] = dr1["img_url"]; //发布时间
                        dr["click"] = dr1["click"]; //点击量
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 查询数据 用于普通检索数据库
        /// </summary>
        public DataTable get_search_list(int site_id, string channel_name, string tags, string keyword, int page, int _pagesize, out int _totalcount)
        {
            //创建一个DataTable
            string link_url = null;
            DataTable dt = null;
            //创建一个DataSet,判断是使用Tags还是关健字查询
            DataTable oldData = new DataTable();
            if (!string.IsNullOrEmpty(tags))
            {
                oldData = new BLL.article().ArticleSearch(channel_name, tags, _pagesize, page, string.Empty, "add_time desc,id desc", out _totalcount).Tables[0];
            }
            else
            {
                oldData = new BLL.article().ArticleSearch(site_id, _pagesize, page, "(title like '%" + keyword + "%' or zhaiyao like '%" + keyword + "%')", "add_time desc,id desc", out _totalcount).Tables[0];
            }

            //复制结构
            dt = oldData.Clone();
            dt.Columns.Add("link_url", Type.GetType("System.String"));
            if (oldData.Rows.Count > 0)
            {
                foreach (DataRow dr in oldData.Rows)
                {
                    link_url = get_url_rewrite(Utils.StrToInt(dr["channel_id"].ToString(), 0), dr["call_index"].ToString(), Utils.StrToInt(dr["id"].ToString(), 0));
                    if (!string.IsNullOrEmpty(link_url))
                    {
                        DataRow row = dt.NewRow();
                        row["id"] = dr["id"];
                        row["channel_id"] = dr["channel_id"];
                        row["category_id"] = dr["category_id"];
                        row["title"] = dr["title"];
                        row["call_index"] = dr["call_index"];
                        row["zhaiyao"] = dr["zhaiyao"];
                        row["img_url"] = dr["img_url"];
                        row["link_url"] = link_url;
                        row["add_time"] = dr["add_time"];
                        row["click"] = dr["click"];
                        dt.Rows.Add(row);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="channel_name">频道名</param>
        /// <param name="keyword">关键词</param>
        /// <param name="islucene">是否启用Lucene</param>
        /// <param name="page">页码</param>
        /// <param name="_pagesize">页容量</param>
        /// <param name="_totalcount">总记录数</param>
        /// <returns></returns>
        public DataTable get_search_list(int channel_id, int category_id, string channel_name, string keyword, bool islucene, int page, int pagesize, out int totalcount)
        {
            if (islucene)
            {
                //return get_lucene_list(channel_id, category_id, keyword, page, pagesize, out totalcount, site.id);
            }
            else
            {
                
            }
            return get_search_list(site.id, channel_name, "", keyword, page, pagesize, out totalcount);
        }
    }
}
